/**
 ** This file is part of FK Klistret Extensions. FK Klistret Extensions 
 ** is free software: you can redistribute it and/or modify it under the 
 ** terms of the GNU General Public License as published by the Free 
 ** Software Foundation, either version 3 of the License, or (at your option) 
 ** any later version.

 ** FK Klistret Extensions is distributed in the hope that it will be 
 ** useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** General Public License for more details. You should have received a
 ** copy of the GNU General Public License along with FK Klistret Extensions. 
 ** If not, see <http://www.gnu.org/licenses/>
 */
package se.fk.klistret.migrate.service;

import java.util.Date;

import javax.xml.bind.JAXBException;

import se.fk.klistret.migrate.utility.FileHelper;

import com.klistret.cmdb.ci.commons.Origin;
import com.klistret.cmdb.ci.element.context.Timeframe;
import com.klistret.cmdb.ci.pojo.Element;
import com.klistret.cmdb.ci.pojo.ElementType;

public class TimeframeServiceImpl extends BaseElementService implements
		TimeframeService {
	
	/**
	 * Trameframe type
	 */
	protected ElementType timeframeType;

	@Override
	protected Element unmarshall(String line, String directory)
			throws JAXBException {
		String[] values = line.split(",");

		String originId = values[0];
		String name = FileHelper.removeQuotes(values[1]);
		Date fromTimeStamp = FileHelper.parseDate(FileHelper
				.removeQuotes(values[2]));
		Date toTimeStamp = FileHelper.parseDate(FileHelper
				.removeQuotes(values[3]));
		Date createTimeStamp = FileHelper.parseDate(FileHelper
				.removeQuotes(values[4]));
		Date updateTimeStamp = FileHelper.parseDate(FileHelper
				.removeQuotes(values[5]));

		Element element = new Element();
		element.setType(getTimeframeType());
		element.setName(name);
		element.setFromTimeStamp(fromTimeStamp);
		element.setToTimeStamp(toTimeStamp);
		element.setCreateTimeStamp(createTimeStamp);
		element.setUpdateTimeStamp(updateTimeStamp);

		Timeframe configuration = new Timeframe();
		configuration.setName(element.getName());

		Origin origin = new Origin();
		origin.setName("Matrix");
		origin.setIdentification(originId);
		configuration.setOrigin(origin);

		element.setConfiguration(configuration);
		return element;
	}

	/**
	 * Get element type
	 */
	protected ElementType getTimeframeType() {
		if (timeframeType == null)
			timeframeType = elementTypeService
				.get("{http://www.klistret.com/cmdb/ci/element/context}Timeframe");
		
		return timeframeType;
	}

}
